package com.iuie.ns.system.service.report.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.utils.ListUtils;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.system.mapper.report.ReportParamsMapper;
import com.iuie.ns.system.service.report.IReportParamsService;
import com.iuie.ns.system.vo.report.ReportParamsVO;

@Service("reportParamsService")
public class ReportParamsServiceImpl implements IReportParamsService {

	@Resource
	private ReportParamsMapper reportParamsMapper;
	
	@Override
	public Integer deleteByReportId(String reportId) {
		if (StringUtils.isBlank(reportId)) {
			throw new ServiceException("报表id不能为空！");
		}
		return reportParamsMapper.deleteByReportId(reportId);
	}

	@Override
	public List<ReportParamsVO> findByPage(Page page, ReportParamsVO condition) {
		PageHelper.startPage(page.getPage(), page.getRows(), true);
		List<ReportParamsVO> list = reportParamsMapper.findByCondition(condition);
		page.loadData(list);
		return list;
	}
	
	@Override
	public Integer deleteParamByReportIdAndParamKey(String reportId, String paramKey) {
		if (StringUtils.isBlank(reportId)) {
			throw new ServiceException("报表id不能为空！");
		}
		if (StringUtils.isBlank(paramKey)) {
			throw new ServiceException("参数Key不能为空！");
		}
		return reportParamsMapper.deleteParamByReportIdAndParamKey(reportId, paramKey);
	}
	
	@Override
	public void addOne(ReportParamsVO item, String oldParamKey) {
		// paramKey, reportId, paramValue is not null
		if (StringUtils.isBlank(item.getReportId())) {
			throw new ServiceException("报表id不能为空！");
		}
		if (StringUtils.isBlank(item.getParamKey())) {
			throw new ServiceException("参数Key不能为空！");
		}
		if (StringUtils.isBlank(item.getParamValue())) {
			throw new ServiceException("参数值不能为空！");
		}
		if (!StringUtils.match(item.getParamKey(), "^[a-zA-Z_][a-zA-Z0-9_.]{1,100}$")) {
			throw new ServiceException("格式不合法<br>格式：以英文26个字母开头，大小写随意，内容仅能为100个英文字母或下划线或小数点！");
		}
		// 1.如果修改paramKey，那么应该先delete，后add；否则，直接update
		if (StringUtils.equals(item.getParamKey(), oldParamKey)) {
			reportParamsMapper.updateOne(item);
		} else {
			reportParamsMapper.deleteParamByReportIdAndParamKey(item.getReportId(), oldParamKey);
			reportParamsMapper.addOne(item);
		}
	}
	
	@Override
	public ReportParamsVO findById(String reportId, String paramKey) {
		if (StringUtils.isBlank(reportId)) {
			throw new ServiceException("报表id不能为空！");
		}
		if (StringUtils.isBlank(paramKey)) {
			throw new ServiceException("参数Key不能为空！");
		}
		return reportParamsMapper.findById(reportId, paramKey);
	}
	
	@Override
	public Map<String, String> findMapOfParams(String reportId) {
		if (StringUtils.isBlank(reportId)) {
			throw new ServiceException("报表id不能为空！");
		}
		List<ReportParamsVO> list = reportParamsMapper.findByReportId(reportId);
		Map<String, String> map = ListUtils.toMap(list, "paramKey", "paramValue");
		return map;
	}
}
